/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.multi.client.account.service.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.multi.client.account.api.entity.MiniAccountExtends;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccount;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccountShop;
import com.medusa.gruul.multi.client.account.service.mp.service.IMiniAccountExtendsService;
import com.medusa.gruul.multi.client.account.service.mp.service.IMultiClientAccountService;
import com.medusa.gruul.multi.client.account.service.mp.service.IMultiClientAccountShopService;
import com.medusa.gruul.multi.client.account.service.service.AccountDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 张治保
 * date 2021/12/10
 */
@Service
@RequiredArgsConstructor
public class AccountDataServiceImpl implements AccountDataService {

    private final IMultiClientAccountService multiClientAccountService;
    private final IMultiClientAccountShopService multiClientAccountShopService;
    private final IMiniAccountExtendsService miniAccountExtendsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncAccountExtendsByMobile(String mobile) {
        MultiClientAccount oldAccount = multiClientAccountService.lambdaQuery()
            .select(MultiClientAccount::getId)
            .eq(MultiClientAccount::getMobile, mobile)
            .one();
        if (oldAccount == null) {
            return;
        }
        MultiClientAccountShop shop = multiClientAccountShopService.lambdaQuery()
            .eq(MultiClientAccountShop::getAccountId, oldAccount.getId())
            .one();
        if (shop == null) {
            return;
        }

        MiniAccountExtends nowData = miniAccountExtendsService.lambdaQuery()
            .eq(MiniAccountExtends::getUserId, ISecurity.user().must().getUserId())
            .one();
        MiniAccountExtends oldData = miniAccountExtendsService.lambdaQuery()
            .eq(MiniAccountExtends::getUserId, shop.getUserId())
            .one();

        MiniAccountExtends accountExtends = this.syncAccountExtends(nowData, oldData);
        if (accountExtends == null){
            return;
        }
        miniAccountExtendsService.updateById(accountExtends);
        multiClientAccountService.removeById(oldAccount.getId());
    }

    private MiniAccountExtends syncAccountExtends(MiniAccountExtends nowData, MiniAccountExtends oldData) {
        if (nowData == null) {
            return oldData;
        }
        if (oldData == null) {
            return nowData;
        }
        nowData.setIntegral(nowData.getIntegral().add(oldData.getIntegral()))
            .setIntegralTotal(nowData.getIntegralTotal().add(oldData.getIntegralTotal()))
            .setClockNum(this.getMaxNumber(nowData.getClockNum(), oldData.getClockNum()))
            .setLastDealTime(this.getLatestTime(nowData.getLastDealTime(), oldData.getLastDealTime()))
            .setIsBlacklist(this.getTrueBoolean(nowData.getIsBlacklist(), oldData.getIsBlacklist()))
            .setConsumeNum(this.sumNumber(nowData.getConsumeNum(), oldData.getConsumeNum()))
            .setLastLoginTime(this.getLatestTime(nowData.getLastLoginTime(),oldData.getLastLoginTime()))
            .setConsumeTotleMoney(NumberUtil.add(nowData.getConsumeTotleMoney(),oldData.getConsumeTotleMoney()))
            .setJoinBlacklistTime(this.getLatestTime(nowData.getJoinBlacklistTime(),oldData.getJoinBlacklistTime()))
            .setSupplyBonus(NumberUtil.add(nowData.getSupplyBonus(),oldData.getSupplyBonus()))
            .setFreezeBonus(NumberUtil.add(nowData.getFreezeBonus(),oldData.getFreezeBonus()))
            .setRebateBonus(NumberUtil.add(nowData.getRebateBonus(),oldData.getRebateBonus()));
        nowData.setCreateTime(this.getEarliestTime(nowData.getCreateTime(),oldData.getCreateTime()))
            .setUpdateTime(LocalDateTime.now());
        return nowData;
    }


    /**
     * 获取两个Integer中最大的树
     */
    private Integer getMaxNumber(Integer a, Integer b) {
        return a == null ? b : b == null ? a : NumberUtil.max(a, b);
    }

    /**
     * 获取两个time中最晚的
     */
    private LocalDateTime getLatestTime(LocalDateTime a, LocalDateTime b) {
        return a == null ? b : b == null ? a : a.isBefore(b) ? b : a;
    }

    /**
     * 获取两个time中最早的
     */
    private LocalDateTime getEarliestTime(LocalDateTime a, LocalDateTime b) {
        return a == null ? b : b == null ? a : a.isBefore(b) ? a : b;
    }
    /**
     * 获取两个Integer中最大的树
     */
    private Integer sumNumber(Integer a, Integer b) {
        return a == null ? b : b == null ? a : a + b;
    }

    /**
     * 获取两个Integer相加
     */
    private Boolean getTrueBoolean(Boolean a, Boolean b) {
        return a == null ? b : b == null ? a : a ? a : b;
    }


}
